﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;

namespace Marbles.Data
{
    public class BaseDataSource
    {

        public const int BinaryStreamBufferSize = 4096;

        public byte[] ReadBinaryStream(Stream Stream)
        {
            BinaryReader sr = new BinaryReader(Stream);
            byte[] data = new byte[Stream.Length];
            int offset = 0;
            int remaining = data.Length;

            while (remaining > 0)
            {
                int read = sr.Read(data, offset, BinaryStreamBufferSize);
                if (read <= 0) { throw new EndOfStreamException("End of stream reached."); }
                remaining -= read;
                offset += read;
            }

            sr.Close();
            return data;
        }

        public static Type GetNativeType(SqlDbType Type)
        {
            switch (Type)
            {
                case SqlDbType.BigInt: return typeof(Int64);
                case SqlDbType.Bit: return typeof(Boolean);
                case SqlDbType.Char: return typeof(String);
                case SqlDbType.Date: return typeof(DateTime);
                case SqlDbType.DateTime: return typeof(DateTime);
                case SqlDbType.DateTime2: return typeof(DateTime);
                case SqlDbType.DateTimeOffset: return typeof(DateTimeOffset);
                case SqlDbType.VarBinary: return typeof(Byte[]);
                case SqlDbType.Float: return typeof(Double);
                case SqlDbType.Binary: return typeof(Byte[]);
                case SqlDbType.Int: return typeof(Int32);
                case SqlDbType.Money: return typeof(Decimal);
                case SqlDbType.NChar: return typeof(String);
                case SqlDbType.NText: return typeof(String);
                case SqlDbType.Decimal: return typeof(Decimal);
                case SqlDbType.NVarChar: return typeof(String);
                case SqlDbType.Real: return typeof(Single);
                case SqlDbType.Timestamp: return typeof(Byte[]);
                case SqlDbType.SmallInt: return typeof(Int16);
                case SqlDbType.SmallMoney: return typeof(Decimal);
                case SqlDbType.Variant: return typeof(Object);
                case SqlDbType.Text: return typeof(String);
                case SqlDbType.Time: return typeof(TimeSpan);
                case SqlDbType.TinyInt: return typeof(Byte);
                case SqlDbType.UniqueIdentifier: return typeof(Guid);
                case SqlDbType.VarChar: return typeof(String);
                case SqlDbType.Xml: return typeof(System.Xml.XmlDocument);
            }

            return typeof(object);
        }

        public static Type GetNativeType(string Name)
        {
            return GetNativeType(GetSqlDbType(Name));
        }

        public static SqlDbType GetSqlDbType(string name)
        {
            switch (name.ToLower().Trim())
            {
                case "bigint": return SqlDbType.BigInt;
                case "binary": return SqlDbType.VarBinary;
                case "bit": return SqlDbType.Bit;
                case "char": return SqlDbType.Char;
                case "date": return SqlDbType.Date;
                case "datetime": return SqlDbType.DateTime;
                case "datetime2": return SqlDbType.DateTime2;
                case "datetimeoffset": return SqlDbType.DateTimeOffset;
                case "decimal": return SqlDbType.Decimal;
                case "float": return SqlDbType.Float;
                case "image": return SqlDbType.Binary;
                case "int": return SqlDbType.Int;
                case "money": return SqlDbType.Money;
                case "nchar": return SqlDbType.NChar;
                case "ntext": return SqlDbType.NText;
                case "numeric": return SqlDbType.Decimal;
                case "nvarchar": return SqlDbType.NVarChar;
                case "real": return SqlDbType.Real;
                case "rowversion": return SqlDbType.Timestamp;
                case "smalldatetime": return SqlDbType.DateTime;
                case "smallint": return SqlDbType.SmallInt;
                case "smallmoney": return SqlDbType.SmallMoney;
                case "sql_variant": return SqlDbType.Variant;
                case "text": return SqlDbType.Text;
                case "time": return SqlDbType.Time;
                case "timestamp": return SqlDbType.Timestamp;
                case "tinyint": return SqlDbType.TinyInt;
                case "uniqueidentifier": return SqlDbType.UniqueIdentifier;
                case "varbinary": return SqlDbType.VarBinary;
                case "varchar": return SqlDbType.VarChar;
                case "xml": return SqlDbType.Xml;
            }

            return SqlDbType.Variant;
        }


    }
}
